<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParam" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="queryParam.roleName"
          placeholder="请输入角色名称"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="searchQuery"
        >查询角色</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-refresh-right"
          @click="reset"
        >重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
        >新增角色</el-button>
      </el-form-item>
      <el-form-item>
        <el-upload
          ref="upload"
          :action="url.importExcelUrl"
          :limit="1"
          :show-file-list="false"
          :on-success="handleImportExcel"
          accept=".xls,.xlsx"
        >
          <el-button type="primary" icon="el-icon-download">导入角色</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-upload2"
          @click="handleExportExcel"
        >导出角色</el-button>
      </el-form-item>
      <el-form-item v-if="selectedIds.length > 0">
        <el-button
          type="danger"
          icon="el-icon-delete"
          @click="handleDeleteBatch()"
        >批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
      @select="handleSelect"
      @select-all="handleSelectAll"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" label="角色编号" width="250">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="角色名称" width="250" align="center">
        <template slot-scope="scope">
          {{ scope.row.roleName }}
        </template>
      </el-table-column>
      <el-table-column label="角色编码" width="250" align="center">
        <template slot-scope="scope">
          {{ scope.row.roleCode }}
        </template>
      </el-table-column>
      <el-table-column label="角色描述" width="450" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" width="250" align="center">
        <template slot-scope="scope">
          {{ scope.row.gmtCreate }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="280" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            round
            size="small"
            @click.native.prevent="handleEdit(scope.$index, scope.row)"
          >
            编辑
          </el-button>
          <el-popconfirm
            confirm-button-text="确认"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @onConfirm="handleDelete(scope.row.id)"
          >
            <el-button slot="reference" type="danger" round size="small">
              删除
            </el-button>
          </el-popconfirm>
          <el-button
            type="success"
            round
            size="small"
            @click.native.prevent="handleDrawer(scope.row.id)"
          >
            权限分配
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="text-align: right">
      <el-pagination
        :current-page="currentPage"
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="pageTotal"
        @current-change="handleCurrentChange"
      />
    </div>
    <RoleModel ref="modalForm" />
    <RoleDrawer ref="drawer" :permissions="permissions" />
  </div>
</template>

<script>
import RoleModel from '@/views/ams/system/module/RoleModel'
import { RCListMixin } from '@/mixins/RCListMixin'
import RoleDrawer from '@/views/ams/system/RoleDrawer'
import permission from '@/api/system/permission'
export default {
  components: { RoleModel, RoleDrawer },
  mixins: [RCListMixin],
  data() {
    return {
      permissions: [],
      url: {
        list: '/system/sys-role/list',
        delete: '/system/sys-role/deleteById',
        deleteBatch: '/system/sys-role/deleteBatch',
        importExcelUrl: `${process.env.VUE_APP_BASE_API}/system/sys-role/importExcel`,
        exportExcelUrl: `/system/sys-role/exportExcel`
      },
      excelName: '角色列表'
    }
  },
  created() {
    this.getAllPermission()
  },
  methods: {
    onSubmit() {
      console.log('submit!')
    },
    handleDrawer(roleId) {
      console.log('handleDrawer')
      this.roleId = roleId
      this.$refs.drawer.visible = true
      this.$refs.drawer.roleId = roleId
      this.$refs.drawer.selectedIds = []
      this.$refs.drawer.getTableData()
      this.$refs.drawer.getRolePermission()
    },
    getAllPermission() {
      permission.getAllPermission().then((res) => {
        console.log(res)
        this.permissions = res.result
      })
    }
  }
}
</script>
